import argparse
import re

def cou_char(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        txt = file.read()
        return len(txt)

def cou_w(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        txt = file.read()
        num_w = re.findall(r'\b\w+\b', txt)
        return len(num_w)

def cou_sen(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        txt = file.read()
        cou_sen = re.split(r'[.!?]', txt)
        cou_sen = [i.strip() for i in cou_sen if i.strip()]
        return len(cou_sen)


def main():
    goal = argparse.ArgumentParser(description="文本计数统计")
    goal.add_argument("-c", dest="cou_char", action="store_true", help="统计字符数")
    goal.add_argument("-w", dest="cou_w", action="store_true", help="统计单词数")
    goal.add_argument("-s", dest="cou_sen", action="store_true", help="统计句子数")
    goal.add_argument("file", help="目标文件")

    res = goal.parse_args()

    if not res.cou_char and not res.cou_w and not res.cou_sen:
        print("选择要统计内容：-c 统计字符数；-w 统计单词数；-s 统计句子数")
        return

    try:
        if res.cou_char:
            num_char = cou_char(res.file)
            print(f"字符数为: {num_char}")

        if res.cou_w:
            num_w = cou_w (res.file)
            print(f"单词数为: {num_w}")

        if res.cou_sen:
            num_sen = cou_sen(res.file)
            print(f"句子数为: {num_sen}")

    except FileNotFoundError:
        print("文本文件 '{res.file}' 不存在/错误")

if __name__ == "__main__":
    main()